package com.qqtech.qquan.homemaking.controller;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.homemaking.enums.HomemakingBaseConst;
import com.qqtech.qquan.homemaking.enums.HomemakingTimeTypeDictEnum;
import com.qqtech.qquan.homemaking.model.HomemakingBase;
import com.qqtech.qquan.homemaking.model.HomemakingBaseVo;
import com.qqtech.qquan.homemaking.model.HomemakingTime;
import com.qqtech.qquan.homemaking.model.HomemakingTimeVo;
import com.qqtech.qquan.homemaking.service.HomemakingBaseService;
import com.qqtech.qquan.homemaking.service.HomemakingTimeService;
import com.qqtech.qquan.sys.controller.BaseAgentController;
import com.qqtech.qquan.user.model.UserBase;
import com.qqtech.qquan.user.service.UserBaseService;

@Controller
@RequestMapping(value = "/homemaking/time")
public class HomemakingTimeController extends BaseAgentController {
	@Resource
	private HomemakingTimeService homemakingTimeService;
	
	@Resource
	private HomemakingBaseService homemakingBaseService;
	
	@Resource
	private UserBaseService userBaseService;

	@Override
	protected BaseService<HomemakingTime> getBaseService() {
		return homemakingTimeService;
	}

	@RequestMapping(value = "/list/{baseId}", method = RequestMethod.GET)
	public String list(@PathVariable Integer baseId, Model model) {
		HomemakingBase homemakingBase = homemakingBaseService.getById(baseId);
		model.addAttribute("baseId", baseId);
		if (homemakingBase != null) {
			UserBase userBase = userBaseService.getById(homemakingBase.getCreatorId());
			model.addAttribute("nick", userBase.getNick());
			if (homemakingBase.getTypeId().intValue() == HomemakingBaseConst.RI_CHANG_BAO_JIE_ID) {
				model.addAttribute("isRestMode", 2); //上班模式
			} else {
				model.addAttribute("isRestMode", 1); //休息模式
			}
		}
		String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
		String[] hours = {"7:00","8:00","9:00","10:00","11:00","12:00","13:00","14:00","15:00","16:00","17:00","18:00","19:00","20:00","21:00"};
		List<Map<String, Object>>  dateList = new ArrayList<Map<String,Object>>();
		Calendar calendar1 = Calendar.getInstance();
		Calendar calendar2 = Calendar.getInstance();
		calendar2.add(Calendar.DATE, 7);
		while (calendar1.before(calendar2)) {
			Map<String, Object> dataMap = new HashMap<String, Object>();
			String dateStr = TimeUtil.getStringDateByFormat(calendar1.getTime(),"yyyy-MM-dd");
			List<Map<String, Object>> workList = new ArrayList<Map<String, Object>>();
			
			List<HomemakingTimeVo> selectHourList = homemakingTimeService
					.findByDate(baseId, dateStr, null);
			for (int i = 0; i < hours.length; i++) {
				boolean flag = false;
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("hour", hours[i]);
				for (HomemakingTimeVo t : selectHourList) {
					if (t.getServiceHourStr().equals(hours[i])) {
						flag = true;
						map.put("timeId", t.getId());
						map.put("type", t.getType());
						if (t.getType().intValue() == 1) {
							map.put("typeStr", "约");
						} else if (t.getType().intValue() == 2) {
							map.put("typeStr", "休");
						} else {
							map.put("typeStr", "班");
						}
						map.put("classStr", "workno");
						break;
					}
				}
				if (!flag) {
					map.put("timeId", 0);
					map.put("type", 0);
					if (homemakingBase.getTypeId().intValue() == HomemakingBaseConst.RI_CHANG_BAO_JIE_ID) {
						map.put("typeStr", "休");
					} else {
						map.put("typeStr", "班");
					}
					map.put("classStr", "workyes");
				}
				workList.add(map);
			}
	        int w = calendar1.get(Calendar.DAY_OF_WEEK) - 1;
	        if (w < 0) {
	        	w = 0;
	        }
			dataMap.put("dateStr", dateStr);
			dataMap.put("weekDay", weekDays[w]);
			dataMap.put("workList", workList);
			dateList.add(dataMap);
			calendar1.add(Calendar.DAY_OF_YEAR, 1);
		}
		model.addAttribute("dateList", dateList);
		return "/homemaking/homemakingtime-list";
	}

	/*@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage list(HomemakingTimeQuery query,
			HttpServletRequest request, HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		if (query != null && query.getBaseId() != null) {
			query.setType(HomemakingTimeTypeDictEnum.REST.getCode());
			String rows = (String) request.getParameter("rows"); // 每页条数
			query.setSize(Integer.valueOf(rows));
			Pageable pageKit = new PageKit(query.getPage(), query.getSize(),
					new PageOrder(Direction.DESC, "id"));
			Page<HomemakingTimeVo> page = homemakingTimeService.queryPageList(
					query, pageKit);
			adminPage.setRows(page.getContent());
			adminPage.setTotal(Integer.valueOf(page.getTotalElements() + ""));
		}
		return adminPage;
	}*/
	
	@RequestMapping(value = "/addRestTime",method = RequestMethod.POST)
	@ResponseBody
	public Result addRestTime(@RequestBody HomemakingTimeVo v) {
		Result result = new Result();
		if (v == null || v.getBaseId() == null
				|| StringUtil.isBlank(v.getServiceDateStr())
				|| StringUtil.isBlank(v.getServiceHourStr())) {
			result.addErr("参数不能为空");
		}
		if (result.checkForm()) {
			HomemakingBase homemakingBase = homemakingBaseService.getById(v.getBaseId());
			Calendar calendar = Calendar.getInstance();
			HomemakingTime inser = new HomemakingTime();
			inser.setBaseId(v.getBaseId());
			inser.setServiceDateStr(v.getServiceDateStr());
			if (homemakingBase.getTypeId().intValue() == HomemakingBaseConst.RI_CHANG_BAO_JIE_ID) {//日常保洁是上班时间
				inser.setType(HomemakingTimeTypeDictEnum.WORK.getCode());
			} else {
				inser.setType(HomemakingTimeTypeDictEnum.REST.getCode());
			}
			inser.setServiceHourStr(v.getServiceHourStr());
			inser.setOrderNo("");
			homemakingTimeService.insert(inser);
		}
		return result;
	}

	@RequestMapping(value = "/deleRestTime",method = RequestMethod.POST)
	@ResponseBody
	public Result deleRestTime(@RequestBody HomemakingTimeVo v) {
		Result result = new Result();
		if (v == null || v.getId() == null) {
			result.addErr("参数不能为空");
		}
		if (result.checkForm()) {
			homemakingTimeService.deleteById(v.getId());
		}
		return result;
	}
}
